#include "obstacle.h"

#include <string>
#include <Crypto++/sha.h>

using std::string;

Obstacle::Obstacle(Obstacle::Type type, int size1, int time)
	: type(type), size1(size1), size2(0), time(time), duration(duration)
{
	calculateColor();
}

Obstacle::Obstacle(Type type, int size1, int size2, int time, int duration)
	: type(type), size1(size1), size2(size2), time(time), duration(duration)
{
	calculateColor();
}

Obstacle::Obstacle(const Obstacle & obstacle)
	: type(obstacle.type), size1(obstacle.size1), size2(obstacle.size2),
	  time(obstacle.time), duration(obstacle.duration)
{
	calculateColor();
}

Obstacle::~Obstacle() {}

void Obstacle::calculateColor()
{
	string s = "";
	s += type + size1 + size2 + time + duration;

	CryptoPP::SHA1 sha;
	byte * digest = new byte[sha.DigestSize()];
	sha.CalculateDigest(digest, (const byte *) s.c_str(), s.size());

	color.r = (int) (digest[0]);
	color.g = (int) (digest[1]);
	color.b = (int) (digest[2]);

	delete[] digest;
}

Color Obstacle::getColor()
{
	return color;
}

Obstacle & Obstacle::operator=(const Obstacle & obstacle)
{
	if(this == &obstacle) {
		return *this;
	}

	this->type = obstacle.type;
	this->size1 = obstacle.size1;
	this->size2 = obstacle.size2;
	this->time = obstacle.time;
	this->duration = obstacle.duration;

	return *this;
}

bool operator<(Obstacle & lhs, Obstacle & rhs)
{
	return lhs.time < rhs.time;
}
